package com.lrkj.property.service.impl;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Carport;
import com.lrkj.property.domain.Vehicle;
import com.lrkj.property.mapper.VehicleMapper;
import com.lrkj.property.service.ICarportService;
import com.lrkj.property.service.IVehicleService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-08-16
 */
@Service
public class VehicleServiceImpl implements IVehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private ICarportService carportService;

    @Autowired
    private IOrgTreeService orgTreeService;

    /**
     * 查询车辆管理
     *
     * @param vehicleId 车辆管理ID
     * @return 车辆管理
     */
    @Override
    public Vehicle selectVehicleById(String vehicleId) {
        return vehicleMapper.selectVehicleById(vehicleId);
    }

    /**
     * 查询车辆管理列表
     *
     * @param vehicle 车辆管理
     * @return 车辆管理
     */
    @Override
    public List<Vehicle> selectVehicleList(Vehicle vehicle) {
        return vehicleMapper.selectVehicleList(vehicle);
    }

    /**
     * 新增车辆管理
     *
     * @param vehicle 车辆管理
     * @return 结果
     */
    @Override
    public int insertVehicle(Vehicle vehicle) {
        vehicle.setVehicleId(IdUtils.simpleUUID());
        vehicle.setCreateBy(SecurityUtils.getUsername());
        vehicle.setCreateTime(DateUtils.getNowDate());

        Carport carport = new Carport();
//        carport.setOwnerId(vehicle.getOwnerId());
        carport.setCarportId(vehicle.getCarportId());
        carport.setParkingId(vehicle.getParkingId());
        carport.setCarportState("10003");
        carportService.updateCarport(carport);
        this.setUnitInfo(vehicle);
        return vehicleMapper.insertVehicle(vehicle);
    }

    /**
     * 修改车辆管理
     *
     * @param vehicle 车辆管理
     * @return 结果
     */
    @Override
    public int updateVehicle(Vehicle vehicle) {
        vehicle.setUpdateBy(SecurityUtils.getUsername());
        vehicle.setUpdateTime(DateUtils.getNowDate());
        this.setUnitInfo(vehicle);
        return vehicleMapper.updateVehicle(vehicle);
    }

    /**
     * 批量删除车辆管理
     *
     * @param vehicleIds 需要删除的车辆管理ID
     * @return 结果
     */
    @Override
    public int deleteVehicleByIds(String[] vehicleIds) {
        for (int i = 0; i < vehicleIds.length; i++) {
            Vehicle vehicle = vehicleMapper.selectVehicleById(vehicleIds[i]);
            Carport carport = new Carport();
            carport.setCarportId(vehicle.getCarportId());
            carport.setParkingId(vehicle.getParkingId());
//            carport.setOwnerId("");
            carport.setCarportState("10001");
            carportService.updateCarport(carport);
        }
        orgTreeService.deleteOrgTreeByIds(vehicleIds);
        return vehicleMapper.deleteVehicleByIds(vehicleIds);
    }

    /**
     * 删除车辆管理信息
     *
     * @param vehicleId 车辆管理ID
     * @return 结果
     */
    @Override
    public int deleteVehicleById(String vehicleId) {
        orgTreeService.deleteOrgTreeById(vehicleId);
        return vehicleMapper.deleteVehicleById(vehicleId);
    }

    private void setUnitInfo(Vehicle bean) {
        if (bean.getCarportId()!=null){
        Carport parentBean = carportService.selectCarportById(bean.getCarportId());
        bean.setAncestors(parentBean.getAncestors() + "," + parentBean.getCarportId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getVehicleId(), bean.getPlateNum(), parentBean.getCarportId(), OrgConstants.vehicle, parentBean.getAncestors(), bean.getPlateNum());
        }
    }
}
